package com.yuda.afterexamscore.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.yuda.afterexamscore.domain.RoadMap;
import com.yuda.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 算分流程Controller
 *
 * @author fmx
 * @date 2021年6月18日
 */
@RestController
@RequestMapping("/RoadScore")
@Api("算分流程Controller")
@RefreshScope
public class RoadScoreController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ApiOperation("核验答案下一步下一步")
    @PutMapping("/OneNext")
    public AjaxResult OneNext() {
        //如果状态为2，不改变，如果状态为0，检查填空题数量是否正确,如果正确，设置为0
        List<RoadMap> status = jdbcTemplate.query("SELECT * from road_map", new BeanPropertyRowMapper<>(RoadMap.class));
        if (CollectionUtil.isNotEmpty(status)){
            RoadMap roadMap = status.get(0);
            Integer tiankong = roadMap.getTiankong();
            if (tiankong==0){
                String answerC = jdbcTemplate.queryForObject("SELECT count(1) c from ( SELECT count(1) from tsinghua_formal_answer where type = 6 GROUP BY ques_id,answer ) a", String.class);
                String mappingC = jdbcTemplate.queryForObject("SELECT count(1) c from th_ques_score_mapping where type = 6", String.class);
                if (answerC.equals(mappingC)){
                    jdbcTemplate.update("update road_map set tiankong = 1");
                }
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("导入第三方分值下一步")
    @PutMapping("/TwoNext")
    public AjaxResult TwoNext() {
        //如果状态为2，不改变，如果状态为0，检查填空题数量是否正确,如果正确，设置为0
        List<RoadMap> status = jdbcTemplate.query("SELECT * from road_map", new BeanPropertyRowMapper<>(RoadMap.class));
        if (CollectionUtil.isNotEmpty(status)){
            RoadMap roadMap = status.get(0);
            Integer tiankong = roadMap.getImportScore();
            if (tiankong==0){
                String s = jdbcTemplate.queryForObject("SELECT count(1) from dp_ques_score_excel_nanhao", String.class);
                if (!s.equals("0")){
                    jdbcTemplate.update("update road_map set import_score = 1");
                }
            }
        }
        //首先查看dp_ques_score_excel_nanhao有没有数据
        return AjaxResult.success();
    }

    @ApiOperation("导入第三方分值上一步")
    @PutMapping("/TwoPre")
    public AjaxResult TwoPre() {

        return AjaxResult.success();
    }

    @ApiOperation("科目算分，生成分数表下一步")
    @PutMapping("/ThreeNext")
    public AjaxResult ThreeNext() {
        List<RoadMap> status = jdbcTemplate.query("SELECT * from road_map", new BeanPropertyRowMapper<>(RoadMap.class));
        if (CollectionUtil.isNotEmpty(status)){
            RoadMap roadMap = status.get(0);
            Integer tiankong = roadMap.getCalculateScore();
            if (tiankong==0){
                jdbcTemplate.update("update road_map set calculate_score = 1");
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("科目算分，生成分数表上一步")
    @PutMapping("/ThreePre")
    public AjaxResult ThreePre() {

        return AjaxResult.success();
    }

    @ApiOperation("抽验答案下一步")
    @PutMapping("/FourNext")
    public AjaxResult FourNext() {
        List<RoadMap> status = jdbcTemplate.query("SELECT * from road_map", new BeanPropertyRowMapper<>(RoadMap.class));
        if (CollectionUtil.isNotEmpty(status)){
            RoadMap roadMap = status.get(0);
            Integer tiankong = roadMap.getCheckScore();
            if (tiankong==0){
                jdbcTemplate.update("update road_map set check_score = 1");
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("抽验答案返回")
    @PutMapping("/FourPre")
    public AjaxResult FourPre() {

        return AjaxResult.success();
    }

    @ApiOperation("获取考生成绩返回")
    @PutMapping("/FivePre")
    public AjaxResult FivePre() {

        return AjaxResult.success();
    }
}
